#!/bin/bash
trap 'echo Abort ; exit 1' ERR
err() { echo Error, $@ not found. ; exit 1; }

echo Make Root File System Image.
[ -z `which mkfs.jffs2` ] && err mkfs.jffs2
[ ! -d /opt/9263/rootfs/ ] && err /opt/9263/rootfs/
[ ! -d /opt/9263/deploy/ ] && err /opt/9263/deploy/

rm /opt/9263/temp/ -rf ; mkdir /opt/9263/temp/ ; pushd /opt/9263/temp/

mkdir dev proc root sys tmp usr usr/lib

cp -a $ROOTFS/linuxrc $ROOTFS/bin $ROOTFS/etc $ROOTFS/lib $ROOTFS/phoneMEA $ROOTFS/sbin -t .
cp -a $ROOTFS/usr/bin $ROOTFS/usr/sbin -t usr/
cp -a $ROOTFS/usr/lib/*.so* usr/lib
find usr/lib -name '*gcj*' -exec rm {} \;

# strip:
# 
# bin/busybox
# lib/*.so
# lib/udev/*
# phoneMEA/bin/cvm
# sbin/udevadm
# sbin/udevd
# usr/bin/*
# usr/lib/*.so

for f in bin/busybox `find lib/*.so` `find lib/udev/*` phoneMEA/bin/cvm sbin/udevadm sbin/udevd \
`find usr/bin/*` `find usr/lib/*.so`
do
	if file "$f" | grep -q 'not stripped'
	then arm-linux-strip "$f" ; fi
done

rm -f phoneMEA/dist/*
for f in cldc1.1.jar libmicrobackendfb.so libmidpathnio.so midpath.jar kxml2-2.3.0.jar libmidpathfile.so microbackend.jar
do
	cp $ROOTFS/phoneMEA/dist/$f phoneMEA/dist/
done

popd

mkfs.jffs2 -r /opt/9263/temp/ -e 0x20000 -l -p -n -o /opt/9263/deploy/rootfs.bin --pagesize=0x800
ls /opt/9263/deploy/rootfs.bin -l
